
package GUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author dianacha
 */
public class TablaFacturacion extends JPanel implements ActionListener{
    
    private JLabel columnCod;
    private JLabel columnCant;
    private JTextField cellCod;
    private JTextField cellCant;
    private ArrayList<String> pairNew;
    
    private JButton comprar;
    
    private ArrayList<Integer> noEditable;
    
    private ModeloTablaFact modelTable;
    private JTable table;
    
    // metodo constructor
    
    public TablaFacturacion(ArrayList<String> colNames){
        
        columnCod = new JLabel(colNames.get(0));
        columnCant = new JLabel(colNames.get(3));
        
        modelTable = new ModeloTablaFact(colNames);
        table = new JTable(modelTable);
        
        configComponents();
        organizar();
    }
    
    // metodos privados
    
    private void configComponents(){
        
        cellCod = new JTextField();
        cellCant = new JTextField();
        
        pairNew = new ArrayList<String>();
               
        comprar=new JButton("ok");
        comprar.addActionListener(this);
    }
    
    private void organizar(){
        // panel norte
        JPanel enterData = new JPanel(new GridLayout(2,2,5,0));
	enterData.setBorder(BorderFactory.createEmptyBorder(0,50,0,50));//n,w,s,e
        enterData.add(columnCod);         enterData.add(cellCod);
        enterData.add(columnCant);        enterData.add(cellCant);
        
        JPanel pBoton = new JPanel();
        pBoton.add(comprar);
        
        JPanel panelInsertar = new JPanel(new BorderLayout());
        panelInsertar.setLayout(new BorderLayout());
        panelInsertar.add(enterData, BorderLayout.CENTER);
        panelInsertar.add(pBoton, BorderLayout.SOUTH);
        
        // panel centro
        
        JScrollPane scrollTabla = new JScrollPane(table);
        
        // general
        
        this.setLayout(new BorderLayout());
        this.add(panelInsertar, BorderLayout.NORTH);
        this.add(scrollTabla, BorderLayout.CENTER);
    }
    
    private void capturarPar(){
        pairNew.add(cellCod.getText());
        pairNew.add(cellCant.getText());
    }
    
    private void limpiarCampos(){
        cellCod.setText("");
        cellCant.setText("");
        pairNew.clear();
    }
    
    private void agregarFila(ArrayList<String> pn){
        
        int size = modelTable.getColumnCount();
        Object[] rowNew = new Object[size];
        //"Cod. Producto", "Nombre", "Precio Unidad", "Cantidad", "Subtotal"
        
        for(int i=0;i<size;i++){
            rowNew[i]= "";//
            if(i==0){
                rowNew[i]=Integer.parseInt(pn.get(0));
            }
            if(i==3){
                rowNew[i]=pn.get(1);
            }
            System.out.println("rowNew["+i+"]= "+rowNew[i]);
        }
        
        modelTable.addRow(rowNew);
    }
    
    // ActionListener
    
   @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==comprar){
            capturarPar();
            if((!"".equals(cellCod.getText()))&&(!"".equals(cellCant.getText()))){
                //verificar si el codProducto está en la bd
                agregarFila(pairNew);
                limpiarCampos();
                //si el producto no está en la bd, mostrar mensaje y limpiarCampos()
            }
            else{
                System.err.println("Error: Faltan Datos");
                limpiarCampos();
            }
        }
    }
}
